
if (window.foo) _foo = foo;

// global FOO object : loads external JS scripts dynamically based on the existance of corresponding class name in the DOM
foo = {
	info : '[Client Name Here] Base JavaScript',
	lang : 'en', // change this to an expression that gets the correct language value [en,es,vi,zh]
	log : function() {
		try{console.log.apply('',arguments);} catch(e) {}
	},
	scriptsLoc : location.pathname.split('/').slice(0,-1).join('/') + '/js/modules/',
	mod : {
		'accordion' : {},
		'simplemodal' : {},
		'carousel' : {},
		'datepicker' : {},
		'dismissable_alert' : {},
        'validate' : {},
        'slideshow' : {},
        'scroll_table_bdy' : {},
        'tabs' : {},
        'tablefilter' : {},
		'tablesorter' : {},
		'tooltip' : {},
		'validate' : {}
	},
	loaded : [],
	init : function() {

		// remove non-js message from DOM
		$('.enable-js').remove();

		// preload images so you don't get flickering
		(function(){
			var imagesToLoad = [
				'css/images/bg-tooltip-left.png',
				'css/images/bg-tooltip-right.png',
				'css/images/bg-tooltip-image-left.png',
				'css/images/bg-tooltip-image-right.png'
			];
			$('<div class="no-print" style="position:absolute;left:-9999px;top:-9999px;height:0;width:0;overflow:hidden;"/>').appendTo('body').html('<img src="'+imagesToLoad.join('"/><img src="')+'"/>');
		})();

		// accessibility: [plugin] treat spacebar 'keypress' event the same as the mouse 'click' event
		$.fn.klik = function(f){
			return this.bind('click keypress', function(e) {
				if (!e.keyCode || e.keyCode == 32){
					return f.call(this, e);
				}
			});
		};

		// setup and, if needed, load modules & plugins from other files
		$.each(foo.mod, function(moduleName) {
			$.extend(foo.mod[moduleName], {
				className : '.mod-' + moduleName,
				load : function(m) {
					if($.inArray(moduleName, foo.loaded) < 0) {

						var s = document.createElement('script');
						s.src = foo.scriptsLoc + 'foo.' + moduleName.replace(/_/g,'-') + '.js';
						$('head').append(s);

						(function(){
							if(!foo.mod[moduleName].init){
								setTimeout(arguments.callee, 30);
								return;
							}
							foo.mod[moduleName].init(m);
						})();

						foo.loaded.push(moduleName);
					}
				}
			});

			var module = $('.'+moduleName.replace(/_/g,'-'));
			if (module.size()){
				foo.mod[moduleName].load(module);
			}
		});

		// print entire page - using "print-page" class
		$('.print-page').klik(function() {
			window.print();
		});

		$.fn.inputHint = function(overClass) {
			return this.each(function(){
				var hint = $(this);
				// in this scope, "var f" means the element attribute "for"
				// .. "for" is a reserved word in the ECMA grammar
				var f = hint.attr('for') || hint.attr('title').split('for:').pop();
				if (f) {
					var input = $('#' + f).data('input-hint', hint[0]);

					this.hide = function() {
						hint.css({left:-9999});
					};

					this.show = function() {
						if (input.val() == '') hint.css({left:0});
					};

					// handlers
					input.focus(this.hide);
					input.blur(this.show);
					hint.addClass(overClass).click(function(){input.focus()});

					if (input.val() != '') this.hide();
				}
			});
		};

		// apply plugin
		$('.input-hint').inputHint('hint-over');

	},
	someFunctionToCall: function() {

		// some function you want to call by name
	},
	someOtherFunctionToCall: function() {

		// another function you want to call by name
	}
};

// instantiate FOO object
foo.init();
